package com.ruoyi.zsyh.controller.applet.after.factory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.K100Utils;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * 服务订单 -- 配件单
 */
@Controller
@RequestMapping("/applet/zsyh/serviceOrderPartsFactory")
public class ZsyhServiceOrderPartsFactoryAppletController extends BaseController
{

    @Autowired
    private IZsyhServiceOrderPartsService zsyhServiceOrderPartsService;

    @Autowired
    private IZsyhServiceOrderPartsRelService zsyhServiceOrderPartsRelService;

    @Autowired
    private IZsyhServiceOrderPartsLogService zsyhServiceOrderPartsLogService;

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhServiceOrderLogService zsyhServiceOrderLogService;

    @Autowired
    private IZsyhAfterLogisticsCompanyService zsyhAfterLogisticsCompanyService;

    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;

    @Autowired
    private RedisUtil redisUtils;

    @Autowired
    private K100Utils k100Utils;

    /**
     * 厂家 订单中心 获取配件单列表
     * @param partsState 申请状态 1待审核 2待发货 3已发货 4已返件待确认
     *       申请状态 1待审核 2待发货 3已发货 301已收货 4已返件待确认 5审核拒绝 6已完结(厂家已确认返件)
     * @return
     * @throws Exception
     */
    @RequestMapping("/getFactoryOrderPartsList")
    @ResponseBody
    public AjaxResult getFactoryOrderPartsList(String factoryId, String loginId, String partsOrderId, String partsName, String partsState, String pageNum, String pageSize)throws Exception
    {

        ZsyhServiceOrderParts parts = new ZsyhServiceOrderParts();
        parts.setFactoryId(factoryId);
        parts.setPartsState(partsState);
        parts.setId(partsOrderId);
        Map map = new HashedMap();
        map.put("partsName", partsName);
        parts.setParams(map);
        List<ZsyhServiceOrderParts> listSize = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsList(parts);
        int total = listSize.size();

        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("partsName", partsName);
        parts.setParams(map);
        List<ZsyhServiceOrderParts> partsList = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsList(parts);

        return success("成功", partsList, total);
    }

    /**
     * 厂家 订单中心 获取配件单详情
     * @return
     * @throws Exception
     */
    @RequestMapping("/getFactoryOrderPartsDetails")
    @ResponseBody
    public AjaxResult getFactoryOrderPartsDetails(String orderPartsId)throws Exception
    {
        ZsyhServiceOrderParts orderParts = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsById(orderPartsId);
        String orderId = orderParts.getOrderId();
        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);
        Map map = new HashMap();
        map.put("order", order);
        map.put("orderParts", orderParts);

        return success("成功", map);
    }


    /**
     * 厂家 订单中心 配件单审核
     * @param orderPartsId 配件id
     * @param isReturn 是否需要返回旧配件 1是 0否
     * @param shippingType 返件邮寄付费方式 1寄付，2到付
     * @param partsState 2审核通过 5审核拒绝
     *          申请状态 1待审核 2待发件 3已发件 301已收货 4已返件待确认 5审核拒绝 6已完结(厂家已确认返件)
     * @param partsContent 拒绝原因
     * @param isNewParts 是否发新配件 1是 0否
     * @return
     * @throws Exception
     */
    @RequestMapping("/auditPartsOrder")
    @ResponseBody
    public AjaxResult auditPartsOrder(String orderPartsId, String isNewParts, String isReturn, String shippingType, String partsState, String partsContent)throws Exception
    {
        ZsyhServiceOrderParts orderParts1 = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsById(orderPartsId);

        if(!"1".equals(orderParts1.getPartsState())){
            return error("请勿重复审核");
        }

        ZsyhServiceOrderParts orderParts = new ZsyhServiceOrderParts();
        orderParts.setId(orderPartsId);
        orderParts.setPartsState(partsState);
        orderParts.setPartsContent(partsContent);
        orderParts.setIsReturn(isReturn);
        orderParts.setShippingType(shippingType);
        orderParts.setIsNewParts(isNewParts);

        zsyhServiceOrderPartsService.updateZsyhServiceOrderParts(orderParts);


//        orderParts = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsById(orderPartsId);
        String orderId = orderParts1.getOrderId();

        ZsyhServiceOrderPartsLog partsLog = new ZsyhServiceOrderPartsLog();
        partsLog.setId(IdWorker.getId());
        partsLog.setOrderId(orderId);
        partsLog.setOrderPartsId(orderParts1.getId());
        partsLog.setLogType("2");
        partsLog.setLogContent("厂家审核配件单");
        zsyhServiceOrderPartsLogService.insertZsyhServiceOrderPartsLog(partsLog);
        return success("成功");
    }


    /**
     * 厂家发货 修改物流单号
     * @param orderPartsId
     * @return
     * @throws Exception
     */
    @RequestMapping("/sendOutGoods")
    @ResponseBody
    public AjaxResult sendOutGoods(String orderPartsId, String logisticsId, String logisticsName, String logisticsNumber,
                                        String logisticsProof, String logisticsRemarks)throws Exception
    {
        ZsyhServiceOrderParts orderParts = new ZsyhServiceOrderParts();
        orderParts.setId(orderPartsId);
        orderParts.setLogisticsId(logisticsId);
        orderParts.setLogisticsName(logisticsName);
        orderParts.setLogisticsNumber(logisticsNumber);
        orderParts.setLogisticsProof(logisticsProof);
        orderParts.setLogisticsRemarks(logisticsRemarks);
        orderParts.setPartsState("3");
        zsyhServiceOrderPartsService.updateZsyhServiceOrderParts(orderParts);

        orderParts = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsById(orderPartsId);
        String orderId = orderParts.getOrderId();

        ZsyhServiceOrderPartsLog partsLog = new ZsyhServiceOrderPartsLog();
        partsLog.setId(IdWorker.getId());
        partsLog.setOrderId(orderId);
        partsLog.setOrderPartsId(orderParts.getId());
        partsLog.setLogType("2");
        partsLog.setLogContent("厂家发来新配件");
        zsyhServiceOrderPartsLogService.insertZsyhServiceOrderPartsLog(partsLog);
        return success("成功");
    }

    /**
     * 厂家 确认收货
     * @param orderPartsId
     * @return
     * @throws Exception
     */
    @RequestMapping("/confirmReceipt")
    @ResponseBody
    public AjaxResult confirmReceipt(String orderPartsId)throws Exception
    {
        ZsyhServiceOrderParts orderParts = new ZsyhServiceOrderParts();
        orderParts.setId(orderPartsId);
        orderParts.setPartsState("6");
        zsyhServiceOrderPartsService.updateZsyhServiceOrderParts(orderParts);

        orderParts = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsById(orderPartsId);
        String orderId = orderParts.getOrderId();

        ZsyhServiceOrderPartsLog partsLog = new ZsyhServiceOrderPartsLog();
        partsLog.setId(IdWorker.getId());
        partsLog.setOrderId(orderId);
        partsLog.setOrderPartsId(orderParts.getId());
        partsLog.setLogType("2");
        partsLog.setLogContent("厂家确认收货");
        zsyhServiceOrderPartsLogService.insertZsyhServiceOrderPartsLog(partsLog);
        return success("成功");
    }


    /**
     * 配件发货区域统计图
     * @param factoryId
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping("/getOrderPartsStatistics")
    @ResponseBody
    public AjaxResult getOrderPartsStatistics(String factoryId, String startDate, String endDate)throws Exception
    {
        return success("成功", zsyhServiceOrderPartsService.getOrderPartsStatistics(factoryId, startDate, endDate));
    }



}
